package com.example.supernote.ui.trash

import androidx.compose.foundation.clickable
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Article
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.FolderOpen
import androidx.compose.material.icons.filled.HourglassEmpty
import androidx.compose.material.icons.filled.Undo
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.example.supernote.R
import com.example.supernote.model.manager.AbstractFileComposite
import com.example.supernote.ui.components.UniformButton
import com.example.supernote.ui.components.UniformIcon
import com.example.supernote.ui.manager.BaseCreateDialog
import com.example.supernote.utils.dateToFormatString

@Composable
fun TrashContent(
    uiState: TrashUiState,
    completelyDelete: (file: AbstractFileComposite) -> Unit,
    recovery: (file: AbstractFileComposite) -> Unit,
    modifier: Modifier = Modifier
) {
    val searchPattern = uiState.searchPattern
    val trashFileList = uiState.trashDisplay.filter {
        try {
            if (searchPattern != "")
                it.property!!.name.contains(Regex(searchPattern))
            else
                true
        } catch (e: Exception) {
            it.property!!.name.contains(searchPattern, ignoreCase = true)
        }
    }
    Column(modifier = modifier) {
        HorizontalDivider(modifier = Modifier)
        val itemModifier = Modifier
            .padding(5.dp)
            .heightIn(min = 32.dp)
        if (trashFileList.size != 0) {
            LazyColumn {
                items(trashFileList) { file ->
                    TrashItem(
                        file = file,
                        modifier = itemModifier,
                        completelyDelete = completelyDelete,
                        recovery = recovery
                    )
                }
            }
        } else {
            EmptyDisplay()
        }
    }
}

@Composable
fun TrashItem(
    file: AbstractFileComposite,
    modifier: Modifier = Modifier,
    completelyDelete: (file: AbstractFileComposite) -> Unit,
    recovery: (file: AbstractFileComposite) -> Unit,
) {
    var isExpend by remember {
        mutableStateOf(false)
    }
    var isVisible by remember {
        mutableStateOf(false)
    }
    var operation by remember {
        mutableStateOf("")
    }
    val onConfirm: (AbstractFileComposite) -> Unit = {
        when (operation) {
            "恢复" -> recovery(it)
            "删除" -> completelyDelete(it)
        }
    }
    Column(modifier = modifier.clickable { isExpend = !isExpend }) {
        Row(
            modifier = Modifier
                .fillMaxSize()
                .height(32.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            UniformIcon(imageVector = Icons.Filled.Article)
            Text(
                text = file.property!!.name
            )
            Spacer(modifier = Modifier.weight(1f))
        }
        Row(
            modifier = Modifier
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.End,
        ) {
            Text(
                text = dateToFormatString(file.property!!.lastModified)
            )
        }

        if (isExpend) {
            Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.End) {
                UniformButton(
                    onClick = {
                        operation = "恢复"
                        isVisible = true
                    },
                    imageVector = Icons.Filled.Undo,
                    text = stringResource(id = R.string.trash_recovery)
                )
                Spacer(modifier = Modifier.width(5.dp))
                UniformButton(
                    onClick = {
                        operation = "删除"
                        isVisible = true
                    },
                    imageVector = Icons.Filled.Delete,
                    text = stringResource(id = R.string.trash_delete)
                )
            }
        }
        val context = LocalContext.current
        if (isVisible) {
            ConfirmDialog(
                confirmTitle = String.format(stringResource(id = R.string.trash_operation), operation),
                onConfirm = { onConfirm(file) },
                onCancel = {
                    isVisible = false
                })
            isExpend = !isExpend
        }
    }
}

@Composable
fun EmptyDisplay(modifier: Modifier = Modifier) {
    Row(
        modifier = Modifier
            .padding(10.dp)
            .fillMaxWidth(),
    ) {
        UniformIcon(imageVector = Icons.Filled.HourglassEmpty)
        Spacer(modifier = Modifier.width(5.dp))
        Text(text = stringResource(id = R.string.manager_empty), color = Color.Gray)
    }
}




@Composable
fun ConfirmDialog(
    confirmTitle: String,
    onConfirm: () -> Unit,
    onCancel: () -> Unit
) {
    BaseCreateDialog(
        title = confirmTitle,
        onDismissRequest = onCancel
    ) {
        Row {
            Spacer(modifier = Modifier.weight(1f))
            UniformButton(onClick = {
                onConfirm()
                onCancel()
            }, text = stringResource(id = R.string.manager_confirm))
            Spacer(modifier = Modifier.width(5.dp))
            UniformButton(onClick = onCancel, text = stringResource(id = R.string.manager_cancel))
        }
    }
}

@Composable
fun PromptDialog(confirmTitle: String, onCancel: () -> Unit) {
    BaseCreateDialog(
        title = confirmTitle,
        onDismissRequest = onCancel
    ) {
        Row {
            Spacer(modifier = Modifier.weight(1f))
            UniformButton(onClick = onCancel, text = stringResource(id = R.string.manager_confirm))
        }
    }
}


